package com.tjsinfo.tjsoa.doc.action;

import java.util.Iterator;
import java.util.List;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import org.jbpm.taskmgmt.exe.TaskInstance;
import org.json.JSONArray;
import org.json.JSONObject;

import com.tjsinfo.tjsoa.common.PaginationSupport;
import com.tjsinfo.tjsoa.common.ToolKit;
import com.tjsinfo.tjsoa.doc.bo.ITDocDocBO;
import com.tjsinfo.tjsoa.doc.bo.ITDocDocOpeBO;
import com.tjsinfo.tjsoa.doc.vo.TDocDoc;
import com.tjsinfo.tjsoa.doc.vo.TDocDocOpe;
import com.tjsinfo.tjsoa.workflow.Taskinstance;
import com.tjsinfo.tjsoa.workflow.WorkFlowTemplate;
import com.tjsinfo.tjsoa.workflow.bo.ITaskInstanceBO;

public class DocToAction extends DispatchAction {
	private ITDocDocBO docBo;
	private ITDocDocOpeBO docOpeBo;
	private ITaskInstanceBO taskInstanceBO;
	
	public void setDocBo(ITDocDocBO docBo) {
		this.docBo = docBo;
	}
	public void setDocOpeBo(ITDocDocOpeBO docOpeBo) {
		this.docOpeBo = docOpeBo;
	}
	public void setTaskInstanceBO(ITaskInstanceBO taskInstanceBO) {
		this.taskInstanceBO = taskInstanceBO;
	}
	//跟踪
	public ActionForward track(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
	        throws Exception {
		// TODO Auto-generated method stub
		Long flowid = Long.valueOf(request.getParameter("flowid"));
		PaginationSupport paginationSupport = null;
		List<Taskinstance> list;
		JSONArray array = new JSONArray();
		JSONObject object = new JSONObject();
		if (request.getParameter("start") == null) {
			paginationSupport =  taskInstanceBO.genZong(flowid, 0, 25);
		} else {
			Integer first = Integer.parseInt(request.getParameter("start"));
			paginationSupport = taskInstanceBO.genZong(flowid, first, 25);
		}
		Integer totalProperty = paginationSupport.getTotalCount();
		object.put("totalProperty", totalProperty);
		list = paginationSupport.getItems();
		for (Iterator iterator = list.iterator(); iterator.hasNext();) {
			Taskinstance taskinstance = (Taskinstance) iterator.next();
			JSONObject taskObject = new JSONObject();
			taskObject.put("taskname", taskinstance.getName());
			taskObject.put("username", taskinstance.getTjsUser().getUserName());
			taskObject.put("time", ToolKit.getFormatDate(taskinstance.getEnd(), "yyyy-MM-dd HH:mm"));
			array.put(taskObject);
		}
		object.put("root", array);
		response.getWriter().print(object);
		return null;
	}	
	
	//原件
	public ActionForward oriForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.doc.properties.systemResource_zh_CN");   
		    String s=bundle.getString("oriName"); 		
			String[] oriName = s.split(";");
			request.setAttribute("oriName", oriName);
			
			return mapping.findForward("oriForm");
	}
	
	public ActionForward oriSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			
			return mapping.findForward("oriSelect");
	}	
	
	public ActionForward oriReturn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    Long taskid = Long.valueOf(request.getParameter("taskid"));
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			request.setAttribute("taskid", taskid);
			
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.doc.properties.systemResource_zh_CN");   
		    String s=bundle.getString("oriName"); 		
			String[] oriName = s.split(";");
			request.setAttribute("oriName", oriName);		
			
			return mapping.findForward("oriReturn");
	}	
	
	public ActionForward oriCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			request.setAttribute("taskid", taskid);
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			TaskInstance taskInstance = test.nextStep(taskid);
			if ("办公室登记".equals(taskInstance.getName())) {
				request.setAttribute("check", "office");
				return mapping.findForward("oriCheck");
			} else if ("归还".equals(taskInstance.getName())) {
				request.setAttribute("check", "back");
				return mapping.findForward("oriCheck");
			} 
			
			return super.execute(mapping, form, request, response);
	}
	
	public ActionForward oriCheckSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			
			return mapping.findForward("oriCheckSelect");
	}

	//证件
	public ActionForward cerForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.doc.properties.systemResource_zh_CN");   
		    String s=bundle.getString("cerName"); 		
			String[] cerName = s.split(";");
			request.setAttribute("cerName", cerName);
			
			return mapping.findForward("cerForm");
	}
	
	public ActionForward cerSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			
			return mapping.findForward("cerSelect");
	}	
	
	public ActionForward cerReturn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    Long taskid = Long.valueOf(request.getParameter("taskid"));
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			request.setAttribute("taskid", taskid);
			
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.doc.properties.systemResource_zh_CN");   
		    String s=bundle.getString("cerName"); 		
			String[] cerName = s.split(";");
			request.setAttribute("cerName", cerName);
			
			return mapping.findForward("cerReturn");
	}	
	
	public ActionForward cerCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			request.setAttribute("taskid", taskid);
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			TaskInstance taskInstance = test.nextStep(taskid);
			if ("部长审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "dept");
				return mapping.findForward("cerCheck");
			}else if ("办公室主管审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office");
				return mapping.findForward("cerCheck");
			}else if ("归还".equals(taskInstance.getName())) {
				request.setAttribute("check", "back");
				return mapping.findForward("cerCheck");
			}else if ("办公室主任审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office1");
				return mapping.findForward("cerCheck");
			}
			
			return super.execute(mapping, form, request, response);
	}
	
	public ActionForward cerCheckSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			
			return mapping.findForward("cerCheckSelect");
	}	
	
	//复印件

	public ActionForward copyForm(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.doc.properties.systemResource_zh_CN");   
		    String s=bundle.getString("copyName"); 		
			String[] copyName = s.split(";");
			request.setAttribute("copyName", copyName);
			return mapping.findForward("copyForm");
	}
	
	public ActionForward copySelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			
			return mapping.findForward("copySelect");
	}	
	
	public ActionForward copyReturn(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
		    Long taskid = Long.valueOf(request.getParameter("taskid"));
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			request.setAttribute("taskid", taskid);
			
		    ResourceBundle bundle = ResourceBundle.getBundle("com.tjsinfo.tjsoa.doc.properties.systemResource_zh_CN");   
		    String s=bundle.getString("copyName"); 		
			String[] copyName = s.split(";");
			request.setAttribute("copyName", copyName);
			
			return mapping.findForward("copyReturn");
	}	
	
	public ActionForward copyCheck(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			Long taskid = Long.valueOf(request.getParameter("taskid"));
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			request.setAttribute("taskid", taskid);
			
			WorkFlowTemplate test = new WorkFlowTemplate();
			TaskInstance taskInstance = test.nextStep(taskid);
			if ("部长审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "dept");
				return mapping.findForward("copyCheck");
			}else if ("办公室主任审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office1");
				return mapping.findForward("copyCheck");
			}else if ("办公室主管审批".equals(taskInstance.getName())) {
				request.setAttribute("check", "office");
				return mapping.findForward("copyCheck");
			}
			
			return super.execute(mapping, form, request, response);
	}
	
	public ActionForward copyCheckSelect(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		
			String id = request.getParameter("id");
			
			TDocDoc tdd = docBo.findById(id);
			List<TDocDocOpe> list =  docOpeBo.selectDocId(id);
			
			request.setAttribute("tdd", tdd);
			request.setAttribute("tddoLists", list);
			
			return mapping.findForward("copyCheckSelect");
	}	
}
